package com.smarth.zhizulian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.adapter.MessageCenterTabProvider;
import com.smarth.zhizulian.app.BaseActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.app.EventActivity;
import com.smarth.zhizulian.bean.BaseEntity;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.fragment.MessageAppFragment;
import com.smarth.zhizulian.fragment.NoticeFragment;
import com.smarth.zhizulian.fragment.TestFragment;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.popupwindow.AppDialog;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.view.LazyViewPager;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 18865 on 2018/2/24.
 */

public class MessageCenterActivity extends EventActivity {
    @BindView(R.id.tl_tab)
    SmartTabLayout tlTab;
    @BindView(R.id.vp_viewpager)
    ViewPager vpViewpager;
    private ImageView ivRed;
    public static final String INTENT_RED = "INTENT_RED";
    private boolean mIsRed;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messagecenter);
        ButterKnife.bind(this);
        setTitlebar("消息中心", true, "清空", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AppDialog(mContext).setTitle("温馨提示").setMessage("是否清空？").setOnClickOKListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        KumaToast.show(mContext, "清空");
                        KumaHttp.getService().messageCleanNews().compose(RxCompos.getBEAll2LoginCompos()).subscribe(new ProgressObserver<BaseEntity>(mContext) {
                            @Override
                            public void onNext(BaseEntity baseEntity) {
                                EventBus.getDefault().post(new EventBean(), BaseConfig.EventTag.ON_CLEAR_MESSAGE);
                            }
                        });
                    }
                }).show();
            }
        });
        initTab();

    }

    private void initTab() {
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager()
                , FragmentPagerItems.with(mContext).add("通知", MessageAppFragment.class)
                .add("公告", NoticeFragment.class)
                .create()
        );
        vpViewpager.setOffscreenPageLimit(0);
        vpViewpager.setAdapter(adapter);
        tlTab.setCustomTabView(new MessageCenterTabProvider(mContext));
        tlTab.setViewPager(vpViewpager);
        View view = tlTab.getTabAt(1);
        ivRed = view.findViewById(R.id.iv_red);
        ivRed.setVisibility(getIntent().getBooleanExtra(INTENT_RED, false) ? View.VISIBLE : View.GONE);

        tlTab.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    ivRed.setSelected(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

//    public static void open(Context context) {
//        Intent i = new Intent(context, MessageCenterActivity.class);
//        context.startActivity(i);
//    }

    public static void openRed(Context context, boolean isRed) {
        Intent i = new Intent(context, MessageCenterActivity.class);
        i.putExtra(INTENT_RED, isRed);
        context.startActivity(i);
    }

    @Subscriber(tag = BaseConfig.EventTag.ON_DEAL_NOTICE)
    private void onEventRefreshMine(EventBean eb) {
        ivRed.setVisibility(View.GONE);
    }

}
